package com.ideal.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <h2> http 工具类 </h2>
 *
 * @author ideal-20
 * @date 2023-08-05 00:30
 **/
@Slf4j
public class HttpClientUtil {
    /**
     * <h2> 发送 GET 请求 </h2>
     *
     * @param url    请求的 URL
     * @param params 请求参数
     * @param headers 请求头
     * @return 响应结果
     */
    public static String doGet(String url, Map<String, String> params, Map<String, String> headers) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            if (params != null) {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    uriBuilder.setParameter(entry.getKey(), entry.getValue());
                }
            }
            URI uri = uriBuilder.build();
            HttpGet httpGet = new HttpGet(uri);
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpGet.setHeader(entry.getKey(), entry.getValue());
                }
            }
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            log.error("HttpClientUtil doGet error", e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error("HttpClientUtil doGet close error", e);
            }
        }

        return result;
    }

    /**
     * <h2> 发送 POST 请求 </h2>
     *
     * @param url    请求的 URL
     * @param params 请求参数
     * @param headers 请求头
     * @return 响应结果
     */
    public static String doPost(String url, Map<String, String> params, Map<String, String> headers) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            if (params != null) {
                List<NameValuePair> nameValuePairs = new ArrayList<>();
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                HttpEntity httpEntity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
                httpPost.setEntity(httpEntity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            log.error("HttpClientUtil doPost error", e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error("HttpClientUtil doPost close", e);
            }
        }
        return result;
    }
}
